package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yanglin
 * @create 2023-11-13 9:17 星期一
 * description:
 */
@RestController
@DefaultProperties(defaultFallback = "paymentGlobalFallbackMethod")
public class OrderController {
    
    @Autowired
    private PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfoOk(@PathVariable("id") Integer id){
        return paymentFeignService.paymentInfoOk(id);
    }


//    @HystrixCommand(fallbackMethod = "paymentInfoTimeoutHandler",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
//    })
    //测试 global fallback
    @HystrixCommand
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    public String paymentInfoTimeout(@PathVariable("id") Integer id){
        //模拟异常
        int num = 10 / 0;
        return paymentFeignService.paymentInfoTimeout(id);
    }

    /**
     * paymentInfoTimeout()的备选方法
     * @return
     */
    public String paymentInfoTimeoutHandler(Integer id){
        
        return "我是客户端order80，目前系统繁忙，请稍后再试";
    }
    
    //全局fallback方法（global fallback）
    public String paymentGlobalFallbackMethod(){
        
        return "这是全局兜底方法，系统繁忙，请稍后再试";
        
    }
    
    
}
